#define _CRT_SECURE_NO_WARNINGS
#include"tree.h"
#include<stdio.h>
#include<stdlib.h>

Status InitStack(StackBiTree* S)
{
	(*S).Stack = (BiTree*)malloc(sizeof(BiNode) * MAXSIZE);
	if ((*S).Stack == NULL)return ERROR;
	(*S).base = 0, (*S).top = 0;
	(*S).length = 0;
	return OK;
}
Status StackEmpty(StackBiTree S)
{
	if (!(S.length))return OK;
	return 0;
}
Status Push(StackBiTree* S, BiTree p)
{
	
	S->Stack[S->top] = p;
	//printf("%d", S->Stack[S->top]->data);
	S->top++;
	S->length++;
	return OK;
}
Status Pop(StackBiTree* S, BiTree* q)
{
	if (S->Stack == NULL)return ERROR;
	S->top--;
	*q = S->Stack[S->top];
	S->length--;
	return OK;
}
Status InitTree(BiTree* T)
{
	*T = (BiTree)malloc(sizeof(BiNode));
	if (!T) exit(OVERFLOW);
	scanf("%d", &((*T)->data));
	(*T)->lct = NULL;
	(*T)->rct = NULL;
	return OK;	
}
Status visit(BiTree T)
{
	printf("%d\n", T->data);
	return OK;
}
Status PreOrderTraverse(BiTree T)
{
	if (T == NULL)return OK;	
	else
	{
		visit(T);
		PreOrderTraverse(T->lct);
		PreOrderTraverse(T->rct);
	}
}
Status InOrderTraverse(BiTree T, StackBiTree S)
{
	BiTree p;
	BiTree q = (BiTree)malloc(sizeof(BiNode));
	InitStack(&S);
	p = T;
	while (p || !StackEmpty(S))
	{
		if (p)
		{
			Push(&S, p);
			p = p->lct;
		}
		else
		{
			Pop(&S, &q);			
			printf("this:%d", q->data);
			p = q->rct;
		}
	}
	return OK;
}
